﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using mfe.bll;

namespace mfe_proj.Controls
{
    public partial class AlignmentControl : UserControl
    {
        Enums.AlignmentType _AlignmentType;
        string _ItemGuidID;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }
        public Enums.AlignmentType AlignmentType { get { return _AlignmentType; } set { _AlignmentType = value; } }
        List<ItemRelated> _TempRelatedItems;
        public List<ItemRelated> TempRelatedItems { get { return _TempRelatedItems; } }

        public AlignmentControl()
        {
            InitializeComponent();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Item item = new Item();
            switch (_AlignmentType)
            {
                #region RealPerson or FictionalPerson
                case Enums.AlignmentType.RealPerson:
                case Enums.AlignmentType.FictionalPerson:
                    ///TODO: should wrap all this in a transaction
                    item.Title = txtNew.Text.Trim();
                    item.Content = "";
                    item.ItemGuidID = Guid.NewGuid().ToString();
                    item.ContentType = (int)Enums.ContentType.Author;
                    if (item.Save())
                    {
                        ItemPerson person = new ItemPerson();
                        person.ItemGuidID = item.ItemGuidID;
                        person.PersonGuidID = Guid.NewGuid().ToString();
                        person.Gender = -1;
                        person.CountryID = -1;
                        if (person.Save())
                        {
                            //do some alignment here
                            ItemRelated related = new ItemRelated();
                            related.ItemGuidID = _ItemGuidID;
                            related.RelatedItemGuidID = item.ItemGuidID;
                            if (related.Save())
                            {
                                lbRelatedItems.Items.Add(item.Title);
                                txtNew.Text = string.Empty;
                            }
                        }
                    }
                    break; 
                #endregion
                case Enums.AlignmentType.Work:
                    item.Title = txtNew.Text.Trim();
                    item.Content = "TBA";
                    item.ItemGuidID = Guid.NewGuid().ToString();
                    item.ContentType = (int)Enums.ContentType.Book;
                    if (item.Save())
                    {
                        ItemBook book = new ItemBook();
                        book.ItemGuidID = item.ItemGuidID;
                        book.ItemBookGuidID = Guid.NewGuid().ToString();
                        book.CreatedDate = DateTime.Now;
                        if (book.Save())
                        {
                            //do some alignment here
                            ItemRelated related = new ItemRelated();
                            related.ItemGuidID = _ItemGuidID;
                            related.RelatedItemGuidID = item.ItemGuidID;
                            if (related.Save())
                            {
                                lbRelatedItems.Items.Add(item.Title);
                                txtNew.Text = string.Empty;
                            }
                        }
                    }
                    break;
            }
        }

        private void AlignmentControl_Load(object sender, EventArgs e)
        {
            if (_AlignmentType == Enums.AlignmentType.FictionalPerson ||
                _AlignmentType == Enums.AlignmentType.RealPerson)
            {
                List<Item> relatedItems = Item.RelatedItemCollection(_ItemGuidID);
                foreach (Item item in relatedItems)
                    lbRelatedItems.Items.Add(item);
            }
        }

        private void btnRemoveSelected_Click(object sender, EventArgs e)
        {
            if (lbRelatedItems.SelectedItem != null)
                if (ItemRelated.Delete(ItemGuidID, ((Item)lbRelatedItems.SelectedItem).ItemGuidID))
                    lbRelatedItems.Items.RemoveAt(lbRelatedItems.SelectedIndex);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lbRelatedResults.Items.Clear();
            int contentTypeID = 0; //all
            if (_AlignmentType == Enums.AlignmentType.FictionalPerson ||
                _AlignmentType == Enums.AlignmentType.RealPerson)
            {
                contentTypeID = (int)Enums.ContentType.Author;
            }
            List<Item> searchResult = Item.Search(txtSearch.Text.Trim(), _ItemGuidID, contentTypeID);
            foreach (Item item in searchResult)
                lbRelatedResults.Items.Add(item);
        }

        private void btnAddSelected_Click(object sender, EventArgs e)
        {
            if (lbRelatedResults.SelectedItem == null) return;

            ItemRelated related = new ItemRelated();
            Item temp = (Item)lbRelatedResults.SelectedItem;
            related.RelatedItemGuidID = temp.ItemGuidID;

            if (_ItemGuidID != null && _ItemGuidID != string.Empty)
            {
                //this is an existing item, so save the related item directly to database
                related.ItemGuidID = _ItemGuidID;
                if (related.Save())
                {
                    lbRelatedItems.Items.Add(temp.Title);
                    lbRelatedResults.Items.Remove(temp);
                }
            }
            else
            {
                //this is a new insert, save to a temporary list
                if (_TempRelatedItems == null) _TempRelatedItems = new List<ItemRelated>();
                _TempRelatedItems.Add(related);
            }
        }
    }
}
